package com.project.logistics.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.logistics.bean.*;
import com.project.logistics.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
@Slf4j
@Controller
public class ApplicantController {

    @Autowired
    ApplicantService applicantService;
    @Autowired
    UserService userService;

    @Autowired
    EvaluateService evaluateService;

    @Autowired
    RepairsService repairsService;
    @Autowired
    RepairmanService repairmanService;

    @Autowired
    UserDetailService userDetailService;

    @Autowired
    NoticeService noticeService;



    //报修人首页
    @RequestMapping("/app/main")
    public String mainPage(Model model,HttpSession httpSession){
        User user = (User)httpSession.getAttribute("user");
        List<Repairs> repairsList =userService.getRepairs(user.getUserId());
        int all = repairsService.getAllRepairs().size();
        int undeal = repairsService.getAllUnRepairs().size();
        int deal = repairsService.getAllDealRepairs().size();
        Repairman todayRepairman = repairmanService.getTodayRepairman();
        List<Notice> notices = noticeService.getAllNotice();
        model.addAttribute("notices",notices);
        model.addAttribute("all",all);
        model.addAttribute("todayRepairman",todayRepairman);
        model.addAttribute("undeal",undeal);
        model.addAttribute("deal",deal);
        model.addAttribute("repairsList",repairsList);
        return "/app/main";
    }

//修改密码
    @RequestMapping("/app/checkpwd")
    public String checkPassword(){
        return "/app/password";
    }
//提交修改密码
    @RequestMapping("/app/updatepwd")
    public String updatePassword(String password, String newPassword, HttpSession httpSession, Model model){
        User user = (User)httpSession.getAttribute("user");
        if(user.getPassword().equals(password)){
            applicantService.updatePassword(newPassword,user.getUserId());
            return "/app/main";
        }
        model.addAttribute("msg","密码输入错误");
        return "/app/password";
    }

//查看评价
//    @RequestMapping("/app/checkcom")
//    public String checkComment(Model model,HttpSession httpSession){
//        User user = (User)httpSession.getAttribute("user");
//        List<Evaluate> evaluateList = applicantService.getComments(user.getUserId());
//        model.addAttribute("comments",evaluateList);
//        return "/app/comment/check";
//    }

    //查看评价
    @RequestMapping("/app/checkcom/{repairsId}")
    public String checkComment(@PathVariable int repairsId,Model model){
        System.out.println(repairsId);
//       System.out.println();
        Evaluate evaluate = evaluateService.getCommentById(repairsId);
        Repairs repair = repairsService.getRepair(repairsId);
        Repairman repairman = repairmanService.getRepairmanByUserId(repair.getServiceUserId());
        System.out.println("==dsd==>!!"+repairman);
        System.out.println("==das==>!!"+evaluate);
        System.out.println(evaluate==null);
        model.addAttribute("repairsId",repairsId);
        model.addAttribute("comment",evaluate);
        model.addAttribute("repairman",repairman);
        return "app/comment/insert_update_delete";
    }
    //修改评价
    @RequestMapping("/app/updatecom/{repairsId}")
    public String updateComment(Evaluate evaluate,@PathVariable int repairsId){
        //不允许出现NULL值评价
        System.out.println("!!!"+evaluate);
        if(evaluate.getEvaluateComment()==null) evaluate.setEvaluateComment("");
        evaluate.setRepairsId(repairsId);
        evaluate.setEvaluateTime(new Date());
        System.out.println("!!!"+evaluate);
        evaluateService.updateEvaluate(evaluate);
        System.out.println(evaluateService.getCommentById(evaluate.getRepairsId()));
        return "/app/main";
    }
        //添加评价
        @RequestMapping("/app/insertcom/{repairsId}")
        public String insertComment(Evaluate evaluate,@PathVariable int repairsId){
            //不允许出现NULL值评价
            if(evaluate.getEvaluateComment()==null) evaluate.setEvaluateComment("");
            evaluate.setEvaluateTime(new Date());
            evaluate.setRepairsId(repairsId);
            System.out.println(evaluate);
            evaluateService.insertEvaluate(evaluate);
            return "/app/main";
        }


    //删除评论
    @RequestMapping("/app/deletecom/{repairsId}")
    public String deleteComment(@PathVariable int repairsId){
        evaluateService.deleteComment(repairsId);
        return "/app/main";
    }
    //查看报修
    @RequestMapping("/app/checkrep")
    public String checkRepairs(Model model,HttpSession httpSession){
        User user = (User)httpSession.getAttribute("user");
        List<Repairs> repairsList =repairsService.getRepairsByUserId(user.getUserId());
        model.addAttribute("repairsList",repairsList);
        return "/app/repairs/check";
    }
    //查看添加报修
    @RequestMapping("/app/rep/checkinsert")
    public String checkInsertRepairs(){
        return "/app/repairs/insert";
    }
    //添加报修
    @RequestMapping("/app/rep/submitinsert")
    public String insertRepairs(Repairs repairs, HttpSession httpSession, MultipartFile headerImg, String place_a, String place_b) throws IOException {
        repairs.setRepairsTime(new Date());
        repairs.setRepairsStatus(1);
        repairs.setRepairsUserId(((User)httpSession.getAttribute("user")).getUserId());
        repairs.setRepairsPlace(place_a+place_b);
        System.out.println(repairs);
        String path = "E:\\javaee\\CODE\\logisticsSystem-v3\\src\\main\\resources\\static\\photos\\";
        if (!headerImg.isEmpty()) {
            //获取文件的原文件名
            String originalFilename = headerImg.getOriginalFilename();
            log.info("originalFilename={}", originalFilename);
            //封装的方法，把文件传输到哪个文件下
            headerImg.transferTo(new File(path + originalFilename));
            repairs.setRepairsPhoto("\\photos\\" + originalFilename);
        }
        repairsService.insertRepairs(repairs);
        return "/app/main";
    }
    //修改报修
    @RequestMapping("/app/rep/checkupdate/{repairsId}")
    public String checkUpdateRepairs(@PathVariable int repairsId,Model model,HttpSession httpSession){
        Repairs repairs = repairsService.getRepair(repairsId);
        model.addAttribute("repairs",repairs);
        return "/app/repairs/update";
    }
    //提交修改报修
    @RequestMapping("/app/rep/submitupdate")
    public String submitUpdateRepairs(Repairs repairs,HttpSession httpSession,String place_a,String place_b){
        repairs.setRepairsPlace(place_a+place_b);
        repairsService.updateRepairs(repairs);
        return "/app/main";
    }
    //删除报修
    @RequestMapping("/app/deleterep/{repairsId}")
    public String deleteRepairs(@PathVariable int repairsId){
        repairsService.deleteRepairs(repairsId);
        return "/app/main";
    }


    @RequestMapping("/app/toupdateUserDetail")
    public String toupdateUserDetail(String userId,Model model){
        UserDetail detail = userDetailService.getUserDetailById(userId);

        if (detail == null) {
            detail = new UserDetail();
        }
        model.addAttribute("detail",detail);
        return "/app/userDetailInsert";
    }
    @GetMapping("/app/updateUserDetail")
    public String updateUserDetail(UserDetail userDetail) {
        UserDetail detail = userDetailService.getUserDetailById(userDetail.getUserId());

        if (detail == null) {
            userDetailService.insertUserDetail(userDetail);
        } else {
            userDetailService.updateUserDetail(userDetail);
        }
        return "/app/main";

    }

    }



